using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;namespace LibDldFile{ public class FtpDldFile : IDldFile { private string _ftpLoginUser = string.Empty; private string _ftpLoginPwd = string.Empty; private string _dldFileSaveDir = string.Empty; private List<string> _dldFileList = null; private UInt32 _ftpDldSize = 1024; /// <summary> /// 创建FTP 下载对象(文件保存路径默认为当前程序启动路径) /// </summary> /// <param name="dldFileList">下载文件路径列表</param> /// <param name="ftpLoginUser">FTP 登录用户</param> /// <param name="ftpLoginPwd">FTP 登录密码</param> public FtpDldFile(List<string> dldFileList, string ftpLoginUser, string ftpLoginPwd) { _dldFileList = dldFileList; _ftpLoginPwd = ftpLoginPwd; _ftpLoginUser = ftpLoginUser; } /// <summary> /// 创建FTP 下载对象 /// </summary> /// <param name="dldFileList">下载文件路径列表</param> /// <param name="ftpLoginUser">FTP 登录用户</param> /// <param name="ftpLoginPwd">FTP 登录密码</param> /// <param name="dldFileSaveDir">下载文件保存路径</param> public FtpDldFile(List<string> dldFileList, string ftpLoginUser, string ftpLoginPwd, string dldFileSaveDir) { _dldFileList = dldFileList; _ftpLoginPwd = ftpLoginPwd; _ftpLoginUser = ftpLoginUser; _dldFileSaveDir = dldFileSaveDir; } /// <summary> /// 下载文件保存路径 /// </summary> public string DldFileSaveDir { get { if (_dldFileSaveDir == string.Empty) _dldFileSaveDir = AppDomain.CurrentDomain.BaseDirectory; //确保最后位为\ if (_dldFileSaveDir.Substring(_dldFileSaveDir.Length - 1) != @"\") _dldFileSaveDir = "\\"; return _dldFileSaveDir; } set { _dldFileSaveDir = value; } } /// <summary> /// FTP 登录用户 /// </summary> public string FtpLoginUser { get { if (_ftpLoginUser == string.Empty) throw new Exception("The Ftp LoginUser is null"); return _ftpLoginUser; } set { _ftpLoginUser = value; } } /// <summary> /// FTP 登录密码 /// </summary> public string FtpLoginPwd { get { if (_ftpLoginPwd == string.Empty) throw new Exception("The Ftp LoginPwd is null"); return _ftpLoginPwd; } set { _ftpLoginPwd = value; } } /// <summary> /// 下载FTP 文件每次读取大小 /// </summary> public UInt32 FtpDldSize { get { return _ftpDldSize; } set { _ftpDldSize = value; } } #region IDldFile 成员 public List<string> DldFilePathList { get { if (_dldFileList == null) _dldFileList = new List<string>(); return _dldFileList; } set { _dldFileList = value; } } /// <summary> /// 将指定的FTP 路径中目录路径和文件名称分离,获取文件名称 /// </summary> /// <param name="ftpPath"></param> /// <returns></returns> public static string ConvertPathToFileName(string ftpPath) { string[] strArray = ftpPath.Split(new char[] { '/' }); return strArray[strArray.Length - 1]; } public void Download() { //_dldFileList = new List<string>(); //_dldFileList.Add(@"ftp://192.168.0.21/incoming/1.txt"); //_ftpLoginPwd = "IECAS12345678"; //_ftpLoginUser = "xiangwei"; string curFileName = string.Empty; try { FtpWebRequest request; FtpWebResponse response; foreach (string strFile in DldFilePathList) { curFileName = ConvertPathToFileName(strFile); //校验文件是否存在,如果存在给出提示用户是否覆盖 if (System.IO.File.Exists(DldFileSaveDir curFileName)) { //通过委托获取用户是否同意覆盖 if (!EIsReplace(strFile)) { continue; } } request = (FtpWebRequest)WebRequest.Create(strFile); request.KeepAlive = false; request.Credentials = new NetworkCredential(_ftpLoginUser, _ftpLoginPwd); using (response = (FtpWebResponse)request.GetResponse()) { Stream data = response.GetResponseStream(); //文件大小 long fileLength = response.ContentLength; //文件输出路径 string targetPath = DldFileSaveDir curFileName; //每次下载文件大小 byte[] byteBuffer = new byte[_ftpDldSize]; //当前已下载文件大小 long currentDldBytes = 0; //当前已下载文件百分比 double currentPercentage = 0; //开始下载文件时间 DateTime startTime = DateTime.Now; TimeSpan timeSpan; using (FileStream output = new FileStream(targetPath, FileMode.OpenOrCreate)) { int bytesRead = 0; //上报开始下载文件信息 EDldMessage("开始下载文件:" curFileName " 文件大小:" fileLength ); do { //上报下载文件百分比 EDldPercentage(currentPercentage); bytesRead = data.Read(byteBuffer, 0, byteBuffer.Length); if (bytesRead > 0) { output.Write(byteBuffer, 0, bytesRead); } currentDldBytes = bytesRead; currentPercentage = ((double)currentDldBytes / (double)fileLength) * 100; } while (bytesRead > 0); timeSpan = DateTime.Now - startTime; EDldMessage("下载" curFileName "完成,耗时:" timeSpan.Minutes.ToString() " 分 " timeSpan.Seconds " 秒 " timeSpan.Milliseconds "毫秒"); } } } } catch (WebException e) { throw new WebException("Download " curFileName " error has occored!The exception message is " e.Message); } } #endregion #region IDldFile 成员 public event DelegateIsReplace EIsReplace; public event DelegateDldPercentage EDldPercentage; public event DelegateDldMessage EDldMessage; #endregion }}
下载C# ftp 下载类源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论